package com.example.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 朝花不迟暮
 * @Description: 提到分组，相信大家都会想起SQL的group by。我们经常需要一个List做分组操作。比如，按城市分组用户
 * @Date: 2022/11/4
 */
public class GroupingBySample {

    public static void main(String[] args) {
        List<UserInfo> originUserInfoList = new ArrayList<>();
        originUserInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18, "深圳"));

        originUserInfoList.add(new UserInfo(3L, "捡瓶子的小男孩", 26, "湛江"));
        originUserInfoList.add(new UserInfo(2L, "程序员田螺", 27, "深圳"));

        Map<String, List<UserInfo>> map = originUserInfoList.stream()
                .collect(Collectors.groupingBy(UserInfo::getCity));
        System.out.println(map.toString());
    }
}
